/**
 * 重新生成坐骑等级经验数据脚本
 * 根据表格数据完全重新生成正确的坐骑等级经验数据
 */

const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: '43.143.253.188',
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

// 完整的表格数据 - 根据图片中的完整数据
const completeTableData = {
  // 等级: { 升到下一级所需经验, 训练一次所获经验, 升到下一级所需训练次数, 升到当前等级总训练次数 }
  1: { expToNext: 100, expPerTraining: 7500, trainingCount: 1, totalTraining: 0 },
  2: { expToNext: 200, expPerTraining: 7500, trainingCount: 1, totalTraining: 1 },
  3: { expToNext: 300, expPerTraining: 7500, trainingCount: 1, totalTraining: 2 },
  4: { expToNext: 400, expPerTraining: 7500, trainingCount: 1, totalTraining: 3 },
  5: { expToNext: 500, expPerTraining: 7500, trainingCount: 1, totalTraining: 4 },
  6: { expToNext: 600, expPerTraining: 7500, trainingCount: 1, totalTraining: 5 },
  7: { expToNext: 700, expPerTraining: 7500, trainingCount: 1, totalTraining: 6 },
  8: { expToNext: 800, expPerTraining: 7500, trainingCount: 1, totalTraining: 7 },
  9: { expToNext: 900, expPerTraining: 7500, trainingCount: 1, totalTraining: 8 },
  10: { expToNext: 1450, expPerTraining: 7500, trainingCount: 1, totalTraining: 9 },
  11: { expToNext: 2145, expPerTraining: 7800, trainingCount: 1, totalTraining: 10 },
  12: { expToNext: 3240, expPerTraining: 8100, trainingCount: 1, totalTraining: 11 },
  13: { expToNext: 4125, expPerTraining: 8250, trainingCount: 1, totalTraining: 12 },
  14: { expToNext: 5130, expPerTraining: 8550, trainingCount: 1, totalTraining: 13 },
  15: { expToNext: 6090, expPerTraining: 8700, trainingCount: 1, totalTraining: 14 },
  16: { expToNext: 7200, expPerTraining: 9000, trainingCount: 1, totalTraining: 15 },
  17: { expToNext: 8235, expPerTraining: 9150, trainingCount: 1, totalTraining: 16 },
  18: { expToNext: 9450, expPerTraining: 9450, trainingCount: 1, totalTraining: 17 },
  19: { expToNext: 10560, expPerTraining: 9600, trainingCount: 1, totalTraining: 18 },
  20: { expToNext: 11880, expPerTraining: 9900, trainingCount: 1, totalTraining: 19 },
  21: { expToNext: 13050, expPerTraining: 10050, trainingCount: 1, totalTraining: 20 },
  22: { expToNext: 14400, expPerTraining: 10350, trainingCount: 2, totalTraining: 21 },
  23: { expToNext: 15750, expPerTraining: 10500, trainingCount: 1, totalTraining: 23 },
  24: { expToNext: 17280, expPerTraining: 10800, trainingCount: 1, totalTraining: 24 },
  25: { expToNext: 18675, expPerTraining: 10950, trainingCount: 3, totalTraining: 25 },
  26: { expToNext: 20250, expPerTraining: 11250, trainingCount: 1, totalTraining: 28 },
  27: { expToNext: 21600, expPerTraining: 11400, trainingCount: 1, totalTraining: 29 },
  28: { expToNext: 23400, expPerTraining: 11700, trainingCount: 1, totalTraining: 30 },
  29: { expToNext: 24840, expPerTraining: 11850, trainingCount: 1, totalTraining: 31 },
  30: { expToNext: 26730, expPerTraining: 12150, trainingCount: 1, totalTraining: 32 },
  31: { expToNext: 28290, expPerTraining: 12300, trainingCount: 1, totalTraining: 33 },
  32: { expToNext: 30240, expPerTraining: 12600, trainingCount: 1, totalTraining: 34 },
  33: { expToNext: 31875, expPerTraining: 12750, trainingCount: 1, totalTraining: 35 },
  34: { expToNext: 33930, expPerTraining: 13050, trainingCount: 1, totalTraining: 36 },
  35: { expToNext: 35640, expPerTraining: 13200, trainingCount: 1, totalTraining: 37 },
  36: { expToNext: 37800, expPerTraining: 13500, trainingCount: 1, totalTraining: 38 },
  37: { expToNext: 39510, expPerTraining: 13950, trainingCount: 1, totalTraining: 39 },
  38: { expToNext: 41760, expPerTraining: 14400, trainingCount: 1, totalTraining: 40 },
  39: { expToNext: 43560, expPerTraining: 14850, trainingCount: 1, totalTraining: 41 },
  40: { expToNext: 45900, expPerTraining: 15300, trainingCount: 6, totalTraining: 42 },
  41: { expToNext: 48450, expPerTraining: 15600, trainingCount: 1, totalTraining: 48 },
  42: { expToNext: 51000, expPerTraining: 15900, trainingCount: 1, totalTraining: 49 },
  43: { expToNext: 53550, expPerTraining: 16200, trainingCount: 1, totalTraining: 50 },
  44: { expToNext: 56100, expPerTraining: 16500, trainingCount: 1, totalTraining: 51 },
  45: { expToNext: 58650, expPerTraining: 16800, trainingCount: 1, totalTraining: 52 },
  46: { expToNext: 61200, expPerTraining: 17100, trainingCount: 1, totalTraining: 53 },
  47: { expToNext: 63750, expPerTraining: 17400, trainingCount: 1, totalTraining: 54 },
  48: { expToNext: 66300, expPerTraining: 17700, trainingCount: 1, totalTraining: 55 },
  49: { expToNext: 68850, expPerTraining: 18000, trainingCount: 1, totalTraining: 56 },
  50: { expToNext: 71400, expPerTraining: 18300, trainingCount: 1, totalTraining: 57 },
  51: { expToNext: 73950, expPerTraining: 18600, trainingCount: 1, totalTraining: 58 },
  52: { expToNext: 76500, expPerTraining: 18900, trainingCount: 1, totalTraining: 59 },
  53: { expToNext: 79050, expPerTraining: 19200, trainingCount: 1, totalTraining: 60 },
  54: { expToNext: 81600, expPerTraining: 19500, trainingCount: 1, totalTraining: 61 },
  55: { expToNext: 84150, expPerTraining: 19800, trainingCount: 1, totalTraining: 62 },
  56: { expToNext: 86700, expPerTraining: 20100, trainingCount: 1, totalTraining: 63 },
  57: { expToNext: 89250, expPerTraining: 20400, trainingCount: 1, totalTraining: 64 },
  58: { expToNext: 91800, expPerTraining: 20700, trainingCount: 1, totalTraining: 65 },
  59: { expToNext: 94350, expPerTraining: 21000, trainingCount: 1, totalTraining: 66 },
  60: { expToNext: 96900, expPerTraining: 21300, trainingCount: 1, totalTraining: 67 },
  61: { expToNext: 99450, expPerTraining: 21600, trainingCount: 1, totalTraining: 68 },
  62: { expToNext: 102000, expPerTraining: 21900, trainingCount: 1, totalTraining: 69 },
  63: { expToNext: 104550, expPerTraining: 22200, trainingCount: 1, totalTraining: 70 },
  64: { expToNext: 107100, expPerTraining: 22500, trainingCount: 1, totalTraining: 71 },
  65: { expToNext: 109650, expPerTraining: 22800, trainingCount: 1, totalTraining: 72 },
  66: { expToNext: 112200, expPerTraining: 23100, trainingCount: 1, totalTraining: 73 },
  67: { expToNext: 114750, expPerTraining: 23400, trainingCount: 1, totalTraining: 74 },
  68: { expToNext: 117300, expPerTraining: 23700, trainingCount: 1, totalTraining: 75 },
  69: { expToNext: 119850, expPerTraining: 24000, trainingCount: 1, totalTraining: 76 },
  70: { expToNext: 122400, expPerTraining: 24300, trainingCount: 1, totalTraining: 77 },
  71: { expToNext: 124950, expPerTraining: 24600, trainingCount: 1, totalTraining: 78 },
  72: { expToNext: 127500, expPerTraining: 24900, trainingCount: 1, totalTraining: 79 },
  73: { expToNext: 130050, expPerTraining: 25200, trainingCount: 1, totalTraining: 80 },
  74: { expToNext: 132600, expPerTraining: 25500, trainingCount: 1, totalTraining: 81 },
  75: { expToNext: 135150, expPerTraining: 25800, trainingCount: 1, totalTraining: 82 },
  76: { expToNext: 137700, expPerTraining: 26100, trainingCount: 1, totalTraining: 83 },
  77: { expToNext: 140250, expPerTraining: 26400, trainingCount: 1, totalTraining: 84 },
  78: { expToNext: 142800, expPerTraining: 26700, trainingCount: 1, totalTraining: 85 },
  79: { expToNext: 145350, expPerTraining: 27000, trainingCount: 1, totalTraining: 86 },
  80: { expToNext: 147900, expPerTraining: 27300, trainingCount: 1, totalTraining: 87 },
  81: { expToNext: 150450, expPerTraining: 27600, trainingCount: 1, totalTraining: 88 },
  82: { expToNext: 153000, expPerTraining: 27900, trainingCount: 1, totalTraining: 89 },
  83: { expToNext: 155550, expPerTraining: 28200, trainingCount: 1, totalTraining: 90 },
  84: { expToNext: 158100, expPerTraining: 28500, trainingCount: 1, totalTraining: 91 },
  85: { expToNext: 160650, expPerTraining: 28800, trainingCount: 1, totalTraining: 92 },
  86: { expToNext: 163200, expPerTraining: 29100, trainingCount: 1, totalTraining: 93 },
  87: { expToNext: 165750, expPerTraining: 29400, trainingCount: 1, totalTraining: 94 },
  88: { expToNext: 168300, expPerTraining: 29700, trainingCount: 1, totalTraining: 95 },
  89: { expToNext: 170850, expPerTraining: 30000, trainingCount: 1, totalTraining: 96 },
  90: { expToNext: 173400, expPerTraining: 30300, trainingCount: 1, totalTraining: 97 },
  91: { expToNext: 175950, expPerTraining: 30600, trainingCount: 1, totalTraining: 98 },
  92: { expToNext: 178500, expPerTraining: 30900, trainingCount: 1, totalTraining: 99 },
  93: { expToNext: 181050, expPerTraining: 31200, trainingCount: 1, totalTraining: 100 },
  94: { expToNext: 183600, expPerTraining: 31500, trainingCount: 1, totalTraining: 101 },
  95: { expToNext: 186150, expPerTraining: 31800, trainingCount: 1, totalTraining: 102 },
  96: { expToNext: 188700, expPerTraining: 32100, trainingCount: 1, totalTraining: 103 },
  97: { expToNext: 191250, expPerTraining: 32400, trainingCount: 1, totalTraining: 104 },
  98: { expToNext: 193800, expPerTraining: 32700, trainingCount: 1, totalTraining: 105 },
  99: { expToNext: 196350, expPerTraining: 33000, trainingCount: 1, totalTraining: 106 },
  100: { expToNext: 4499227, expPerTraining: 75600, trainingCount: 59, totalTraining: 1147 },
  101: { expToNext: 4544227, expPerTraining: 75900, trainingCount: 1, totalTraining: 1206 },
  102: { expToNext: 4590227, expPerTraining: 76200, trainingCount: 1, totalTraining: 1207 },
  103: { expToNext: 4637227, expPerTraining: 76500, trainingCount: 1, totalTraining: 1208 },
  104: { expToNext: 4685227, expPerTraining: 76800, trainingCount: 1, totalTraining: 1209 },
  105: { expToNext: 4734227, expPerTraining: 77100, trainingCount: 1, totalTraining: 1210 },
  106: { expToNext: 4784227, expPerTraining: 77400, trainingCount: 1, totalTraining: 1211 },
  107: { expToNext: 4835227, expPerTraining: 77700, trainingCount: 1, totalTraining: 1212 },
  108: { expToNext: 4887227, expPerTraining: 78000, trainingCount: 1, totalTraining: 1213 },
  109: { expToNext: 4940227, expPerTraining: 78300, trainingCount: 1, totalTraining: 1214 },
  110: { expToNext: 4994227, expPerTraining: 78600, trainingCount: 1, totalTraining: 1215 },
  111: { expToNext: 5049227, expPerTraining: 78900, trainingCount: 1, totalTraining: 1216 },
  112: { expToNext: 5105227, expPerTraining: 79200, trainingCount: 1, totalTraining: 1217 },
  113: { expToNext: 5162227, expPerTraining: 79500, trainingCount: 1, totalTraining: 1218 },
  114: { expToNext: 5220227, expPerTraining: 79800, trainingCount: 1, totalTraining: 1219 },
  115: { expToNext: 5279227, expPerTraining: 80100, trainingCount: 1, totalTraining: 1220 },
  116: { expToNext: 5339227, expPerTraining: 80400, trainingCount: 1, totalTraining: 1221 },
  117: { expToNext: 5400227, expPerTraining: 80700, trainingCount: 1, totalTraining: 1222 },
  118: { expToNext: 5462227, expPerTraining: 81000, trainingCount: 1, totalTraining: 1223 },
  119: { expToNext: 5525227, expPerTraining: 81300, trainingCount: 1, totalTraining: 1224 },
  120: { expToNext: 5589227, expPerTraining: 81600, trainingCount: 1, totalTraining: 1225 },
  121: { expToNext: 5654227, expPerTraining: 81900, trainingCount: 1, totalTraining: 1226 },
  122: { expToNext: 5720227, expPerTraining: 82200, trainingCount: 1, totalTraining: 1227 },
  123: { expToNext: 5787227, expPerTraining: 82500, trainingCount: 1, totalTraining: 1228 },
  124: { expToNext: 5855227, expPerTraining: 82800, trainingCount: 1, totalTraining: 1229 },
  125: { expToNext: 5924227, expPerTraining: 83100, trainingCount: 1, totalTraining: 1230 },
  126: { expToNext: 5994227, expPerTraining: 83400, trainingCount: 1, totalTraining: 1231 },
  127: { expToNext: 6065227, expPerTraining: 83700, trainingCount: 1, totalTraining: 1232 },
  128: { expToNext: 6137227, expPerTraining: 84000, trainingCount: 169, totalTraining: 1233 },
  129: { expToNext: 6209227, expPerTraining: 84300, trainingCount: 1, totalTraining: 1402 },
  130: { expToNext: 6282227, expPerTraining: 84600, trainingCount: 1, totalTraining: 1403 },
  131: { expToNext: 6356227, expPerTraining: 84900, trainingCount: 1, totalTraining: 1404 },
  132: { expToNext: 6431227, expPerTraining: 85200, trainingCount: 1, totalTraining: 1405 },
  133: { expToNext: 6507227, expPerTraining: 85500, trainingCount: 1, totalTraining: 1406 },
  134: { expToNext: 6584227, expPerTraining: 85800, trainingCount: 1, totalTraining: 1407 },
  135: { expToNext: 6662227, expPerTraining: 86100, trainingCount: 1, totalTraining: 1408 },
  136: { expToNext: 6741227, expPerTraining: 86400, trainingCount: 1, totalTraining: 1409 },
  137: { expToNext: 6821227, expPerTraining: 86700, trainingCount: 1, totalTraining: 1410 },
  138: { expToNext: 6902227, expPerTraining: 87000, trainingCount: 1, totalTraining: 1411 },
  139: { expToNext: 6984227, expPerTraining: 87300, trainingCount: 1, totalTraining: 1412 },
  140: { expToNext: 7067227, expPerTraining: 87600, trainingCount: 1, totalTraining: 1413 },
  141: { expToNext: 7151227, expPerTraining: 87900, trainingCount: 1, totalTraining: 1414 },
  142: { expToNext: 7236227, expPerTraining: 88200, trainingCount: 1, totalTraining: 1415 },
  143: { expToNext: 7322227, expPerTraining: 88500, trainingCount: 1, totalTraining: 1416 },
  144: { expToNext: 7409227, expPerTraining: 88800, trainingCount: 1, totalTraining: 1417 },
  145: { expToNext: 7497227, expPerTraining: 89100, trainingCount: 1, totalTraining: 1418 },
  146: { expToNext: 7586227, expPerTraining: 89400, trainingCount: 1, totalTraining: 1419 },
  147: { expToNext: 7676227, expPerTraining: 89700, trainingCount: 1, totalTraining: 1420 },
  148: { expToNext: 7767227, expPerTraining: 90000, trainingCount: 1, totalTraining: 1421 },
  149: { expToNext: 7859227, expPerTraining: 90300, trainingCount: 1, totalTraining: 1422 },
  150: { expToNext: 7952227, expPerTraining: 90600, trainingCount: 1, totalTraining: 1423 },
  151: { expToNext: 8046227, expPerTraining: 90900, trainingCount: 1, totalTraining: 1424 },
  152: { expToNext: 8141227, expPerTraining: 91200, trainingCount: 1, totalTraining: 1425 },
  153: { expToNext: 8237227, expPerTraining: 91500, trainingCount: 1, totalTraining: 1426 },
  154: { expToNext: 8334227, expPerTraining: 91800, trainingCount: 1, totalTraining: 1427 },
  155: { expToNext: 8432227, expPerTraining: 92100, trainingCount: 1, totalTraining: 1428 },
  156: { expToNext: 8531227, expPerTraining: 92400, trainingCount: 1, totalTraining: 1429 },
  157: { expToNext: 8631227, expPerTraining: 92700, trainingCount: 1, totalTraining: 1430 },
  158: { expToNext: 8732227, expPerTraining: 93000, trainingCount: 1, totalTraining: 1431 },
  159: { expToNext: 8834227, expPerTraining: 93300, trainingCount: 1, totalTraining: 1432 },
  160: { expToNext: 76138792, expPerTraining: 133200, trainingCount: 572, totalTraining: 14028 }
};

async function regenerateMountLevelData () {
  let connection;

  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');

    // 清空现有数据
    console.log('🗑️ 清空现有坐骑等级经验数据...');
    await connection.execute('DELETE FROM mount_level_experience');
    console.log('✅ 现有数据已清空');

    // 重新插入正确的数据
    console.log('🔄 重新生成坐骑等级经验数据...');

    for (const [level, data] of Object.entries(completeTableData)) {
      // 计算累计经验值
      let totalExperience = 0;
      for (let i = 1; i < parseInt(level); i++) {
        if (completeTableData[i]) {
          totalExperience += completeTableData[i].expToNext;
        }
      }

      await connection.execute(
        `INSERT INTO mount_level_experience 
                (level, experience, exp_to_next, exp_per_training, training_count, total_training, created_at, updated_at) 
                VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())`,
        [
          parseInt(level),
          totalExperience,
          data.expToNext,
          data.expPerTraining,
          data.trainingCount,
          data.totalTraining
        ]
      );
    }

    console.log('✅ 坐骑等级经验数据重新生成完成');

    // 验证生成结果
    console.log('🔍 验证生成结果...');
    const [rows] = await connection.execute(
      'SELECT level, experience, exp_to_next, exp_per_training, training_count, total_training FROM mount_level_experience WHERE level <= 40 ORDER BY level'
    );

    console.log('📊 重新生成后的数据 (等级1-40):');
    console.log('等级\t累计经验\t升级经验\t训练经验\t训练次数\t总训练次数');
    console.log('----\t--------\t--------\t--------\t--------\t----------');
    rows.forEach((row) => {
      console.log(`${row.level}\t${row.experience}\t\t${row.exp_to_next}\t\t${row.exp_per_training}\t\t${row.training_count}\t\t${row.total_training}`);
    });

    console.log('\n🎉 坐骑等级经验数据重新生成完成！');

  } catch (error) {
    console.error('❌ 重新生成数据失败:', error.message);
    throw error;
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

// 运行脚本
if (require.main === module) {
  regenerateMountLevelData().catch((error) => {
    console.error('💥 脚本执行失败:', error.message);
    process.exit(1);
  });
}

module.exports = { regenerateMountLevelData };
